<script src="/private/system/public/js/ajax.js"></script>
<script language="javascript" type="text/javascript">
var gFormElementsV = new Array();
var xmlRequest;

function doLogin()
{
	var username = document.getElementById("lusr");
	var password = document.getElementById("lpwd");
	
	xmlRequest = ajaxRequest((document.URL.indexOf("localhost:8080")!=-1)?"/auth.php":"/auth.php5", 
		loginCallback, "lusr="+username.value+"&lpwd="+password.value);
	if(!xmlRequest)
		alert("Failed");
	
};

function loginCallback()
{
	if(!xmlRequest)
		alert("Error");
	var msg = document.getElementById("lmsg");
	
	if(xmlRequest.readyState != 4)
	{
		msg.innerHTML = "Processing...";
	}
	else
	{
		if (xmlRequest.status == 200) 
		{
			if(!xmlRequest.responseXML)
			{
				msg.innerHTML = "Bad response:<br/>" + 
					xmlRequest.responseText;
			}
			else
			{
				var res = xmlRequest.responseXML.documentElement;
				msg.innerHTML = res.childNodes[1].nodeValue;
				
				if(res.getElementsByTagName('valid')[0].firstChild.nodeValue == "true")
				{
					msg.style.color = "green";
					window.location.reload();
				}
				else
				{
					msg.style.color = "red";
				};
			};
		};
	};
};

function formInit(id)
{
	var form = document.getElementById(id);
	if (!form)
		return;
}

function formValidate(id)
{
	var obj = document.getElementById(id);
	if (!obj)
		return;
	if(obj.value.length <= 0)
	{
		gFormElementsV[id] = false;
		obj.className = "frminvalid";
	}
	else
	{
		gFormElementsV[id] = true;
		obj.className = "frmvalid";
	};
}

function doLogout()
{
	xmlRequest = ajaxRequest((document.URL.indexOf("localhost:8080")!=-1)?"/auth.php":"/auth.php5", 
		logoutCallback, null);
	if(!xmlRequest)
		alert("Failed");
};

function logoutCallback()
{
	if(!xmlRequest)
		alert("Error");
	var msg = document.getElementById("lbutton");
	
	if(xmlRequest.readyState != 4)
	{
		msg.innerHTML = "Processing...";
		msg.disabled = true;
	}
	else
	{
		if (xmlRequest.status == 200) 
		{
			window.location.reload();
		}
		else
		{
			msg.innerHTML = "Logout failed";
			msg.disabled = false;
		}
	};
};
</script>
<div class="applet-collapse" id="portal-login-collapse">[+/-]</div>
<h2 class='applet-title' id='login-applet-title'>Portal Login</h2>
<div id="portal-login-main">
<?php
$usr = $_POST["lusr"];
$pwd = $_POST["lpwd"];
$action = $_GET["loginaction"];

if($gClient->IsValidated())
{
	$drawform = false;
	
	if ($action == "logout")
	{
		dyClearClient();
		header("Location: /");
	}
	else
	{ 
		print("<p>".uiString('welcome',true).", " . $gClient->GetUserId()) ?>

<br/>Priv level: <?php echo($gClient->GetPrivilegesString()) ?></p>
<button onCLick="javascript:doLogout()" id="lbutton">
<?php	uiIcon('back', false);
		print(uiString('logout',true)."</button>");
	};
}
else if(($usr != "") && ($pwd != ""))
{
	$drawform = false;
	
	if(dySetClient($usr, $pwd))
	{
		uiMessage("LoginSuccess");
		header("Location: ".$_SERVER['REQUEST_URI']);
	}
	else
	{
		uiMessage("LoginFailed");
	}
}
else
{
	$drawform = true;
};
if($drawform)
{
?>
<span id="lmsg"></span>
<form name="login" id="login" method="post" action="">
<table width="180"  border="0" cellpadding="0" cellspacing="0" class="form">
  <tr>
	<td colspan="2"><div align="left">Username:</div></td>
  </tr>
  <tr>
	<td colspan="2"><input id="lusr" onChange="formValidate('lusr');" type="text" class="frmvalid" /></td>
  </tr>
  <tr>
	<td colspan="2"><div align="left">Password:</div></td>
  </tr>
  <tr>
	<td colspan="2"><input id="lpwd" onChange="formValidate('lpwd');" type="password" class="frmvalid" /></td>
  </tr>
  <tr>
	<td width="106"><div align="left">Remember me? </div></td>
	<td width="74"><input name="lsave" type="checkbox" id="lsave" value="checkbox" /></td>
  </tr>
  <tr>
	<td><div align="left">Secure?</div></td>
	<td><input name="lsecure" type="checkbox" id="lsecure" value="checkbox" /></td>
  </tr>
</table> 
<input onClick="javascript:doLogin(); return false;" type="submit" value="Login" /> 
</form>
<p align="center">
  
</p>

 <?php	};	?>
</div>